﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Xml.Linq;
using System.Xml;
using System.Xml.XPath;
using System.Data.OleDb;

namespace WW.Redirect
{
    public class GetRedirectUrlSql : WW.Redirect.Interfaces.IGetRedirectUrl    
    {
        public string getRedirectUrl(string lookupCode, WW.Redirect.Interfaces.IConfig configSettings)
        {
            string value = "";
            using (OleDbConnection connection = new OleDbConnection(configSettings.Get("ConnectionString")))
            {
                connection.Open();
                OleDbCommand cmd = connection.CreateCommand();
                cmd.CommandText = configSettings.Get("Query").Replace("%code%",lookupCode);
                OleDbDataReader reader = cmd.ExecuteReader(System.Data.CommandBehavior.SingleRow);
                if (reader.Read())
                {
                    value = reader[0].ToString();
                }
                else
                {
                    value = "";
                }
            }

            return value;
        }
    }
}
